{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Point  销毁\n",
      "92693000 92693000 92693000\n",
      "Point  销毁\n"
     ]
    }
   ],
   "source": [
    "class Point:\n",
    "    '测试 引用计数 python 使用了引用计数 和 循环垃圾收集器'\n",
    "    def __init__(self,x = 0,y = 0):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "        \n",
    "    def __del__(self):\n",
    "        classname = self.__class__.__name__\n",
    "        print classname,' 销毁'\n",
    "        \n",
    "pt1 = Point()\n",
    "pt2 = pt1\n",
    "pt3 = pt1\n",
    "print id(pt1),id(pt2),id(pt3)\n",
    "\n",
    "del pt1\n",
    "del pt2\n",
    "del pt3\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "调用子类构造方法\n",
      "调用子类方法\n",
      "调用父类方法\n",
      "父类属性： 200\n"
     ]
    }
   ],
   "source": [
    "class Parent:\n",
    "    '父类demo'\n",
    "    parentAttr = 100\n",
    "    def __init__(self):\n",
    "        print '调用父类的构造函数'\n",
    "        \n",
    "    def parentMethod(self):\n",
    "        print '调用父类方法'\n",
    "        \n",
    "    def setAttr(self,attr):\n",
    "        Parent.parentAttr = attr\n",
    "\n",
    "    def getAttr(self):\n",
    "        print '父类属性：',Parent.parentAttr\n",
    "        \n",
    "class Child(Parent):\n",
    "    def __init__(self):\n",
    "        print '调用子类构造方法'\n",
    "        \n",
    "    def childMethod(self):\n",
    "        print '调用子类方法'\n",
    "        \n",
    "c = Child()\n",
    "c.childMethod()\n",
    "c.parentMethod()\n",
    "c.setAttr(200)\n",
    "c.getAttr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "调用子类方法\n"
     ]
    }
   ],
   "source": [
    "class Parent:\n",
    "        \n",
    "    def myMethod(self):\n",
    "        print '调用父类方法'\n",
    "        \n",
    "class Child(Parent):\n",
    "        \n",
    "    def myMethod(self):\n",
    "        print '调用子类方法'\n",
    "        \n",
    "c = Child()\n",
    "c.myMethod()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vector (7,8)\n"
     ]
    }
   ],
   "source": [
    "class Vector:\n",
    "    def __init__(self,a,b):\n",
    "        self.a = a\n",
    "        self.b = b\n",
    "        \n",
    "    def __str__(self):\n",
    "        return 'Vector (%d,%d)' % (self.a,self.b)\n",
    "    \n",
    "    def __add__(self,other):\n",
    "        return Vector(self.a + other.a , self.b + other.b)\n",
    "    \n",
    "v1 = Vector(2,10)\n",
    "v2 = Vector(5,-2)\n",
    "\n",
    "print v1 + v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "2\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "class JustCounter:\n",
    "    __secretCount = 0\n",
    "    publicCount = 0\n",
    "    \n",
    "    def count(self):\n",
    "        self.__secretCount += 1\n",
    "        self.publicCount += 1\n",
    "        print self.__secretCount\n",
    "        \n",
    "counter = JustCounter()\n",
    "counter.count()\n",
    "counter.count()\n",
    "\n",
    "print counter.publicCount\n",
    "\n",
    "# 访问私有属性的唯一方式 是在私有属性的前面加上 下划线 和类名\n",
    "print counter._JustCounter__secretCount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
